// pages/walletDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId:0,
    status: [
      {
        type: "all",
        pagination: 1,
        first: true,
        end: false,
        isNone: false
      },
      {
        type: "plus",
        pagination: 1,
        first: true,
        end: false,
        isNone: false
      },
      {
        type: "minus",
        pagination: 1,
        first: true,
        end: false,
        isNone: false
      },
    ],
    list:[[],[],[]]
  },

  navClick(e){
    const that = this;
    // console.log(e)
    that.setData({
      navId:e.currentTarget.dataset.navid
    })
    that.getList(that.data.navId);
  },

  getList(index){
    const that = this;
    let status = that.data.status[index];
    if (status.first) {
      let json = {
        token: app.globalData.token,
        type: status.type,
        is_json: 1,
        p: status.pagination
      }
      let jsonData = app.utils.getSignData(json);
      that.setData({
        hiddenLoading: false
      })
      app.utils.post(app.api.reqUrls.getAccountList, jsonData).then(res => {
        that.setData({
          hiddenLoading: true
        })
        if (res.status == 1) {

          if(res.result.length == 0){
            that.setData({
              [`status[${index}].isNone`]: true
            })
          } else {
            // 格式化时间
            res.result.forEach((item, index) => {
              item.change_time = app.moment(item.change_time * 1000).format('YYYY-MM-DD');
              // item.change_time = app.moment(item.change_time).fromNow();
            })
            that.setData({
              [`list[${index}]`]: [...that.data.list[index], ...res.result],
              [`status[${index}].first`]: false
            })
          }

        } else {
          throw res.msg
        }
      }).catch(err => {
        app.showInfo(err.toString())
      })
    }
  },

  getMoreList(index) {
    const that = this;
    if (!that.data.status[index].end) {
      const that = this;
      let status = that.data.status[index];
      let json = {
        token: app.globalData.token,
        type: status.type,
        is_json: 1,
        p: status.pagination + 1
      }
      let jsonData = app.utils.getSignData(json);
      that.setData({
        hiddenLoading: false
      })
      app.utils.post(app.api.reqUrls.getAccountList, jsonData).then(res => {
        that.setData({
          hiddenLoading: true
        })
        if (res.status == 1) {
          // 格式化时间
          res.result.forEach((item, index) => {
            item.change_time = app.moment(item.change_time*1000).format('YYYY-MM-DD');
            // item.change_time = app.moment(item.change_time).fromNow();
          })
          that.data.list[index] = [...that.data.list[index], ...res.result];
          that.setData({
            [`list[${index}]`]: that.data.list[index]
          })

          // 分页判断
          let len = res.result.length;
          if (len == 0) {
            that.setData({
              [`status[${index}].end`]: true
            })
            wx.showToast({
              title: '没有更多了',
              icon: 'none',
              duration: 1000
            })
          }else {
            that.setData({
              [`status[${index}].pagination`]: status.pagination + 1
            })
          }



        } else {
          throw res.msg
        }
      }).catch(err => {
        app.showInfo(err.toString())
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getList(that.data.navId);

  },
  onShow:function(){
    
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    that.getMoreList(that.data.navId);
  }
})